import pandas as pd
from .Scheme_Type import SchemeType
from datetime import datetime
import glob
import os


class Load_File_Pandas_Tools:
    """
    加载文件数据到pandas中
    """

    def __init__(self, file_path, scheme_type: SchemeType):
        if scheme_type is SchemeType.农户固投调查方案:
            # 存放文件路径
            self.N1问卷_list = []
            self.N2问卷_list = []
            self.N3问卷_list = []
            self.N4问卷_list = []
            self.N5问卷_list = []
            self.N6问卷_list = []
            self.N7问卷_list = []
            self.N卷合并_list = []
            self.账页_list = []
            # 存放文件数据
            self.N1问卷_data_frame = pd.DataFrame([])
            self.N2问卷_data_frame = pd.DataFrame([])
            self.N3问卷_data_frame = pd.DataFrame([])
            self.N4问卷_data_frame = pd.DataFrame([])
            self.N5问卷_data_frame = pd.DataFrame([])
            self.N6问卷_data_frame = pd.DataFrame([])
            self.N7问卷_data_frame = pd.DataFrame([])
            self.N卷合并_data_frame = pd.DataFrame([])
            self.账页_data_frame = pd.DataFrame([])

            # # 输出Excel
            # self.excel_writer_文件 = pd.ExcelWriter(f'{datetime.now().strftime("%Y年%m月%d日%H时%M分")}审核结果.xlsx', engine='xlsxwriter')

    def 初始化工作目录及输出目录(self, work_dir: str, out_dir: str,scheme_type: SchemeType) -> None:
        """
        初始化工作目录和输出目录
        :param work_dir: 存放数据的工作目录路径
        :param out_dir: 输出数据的文件目录路径
        :return: None
        """
        # 遍历用户选择目录下的所有csv文件
        csv_files = glob.glob(os.path.join('./平台导出数据', '*.csv'))
        for file_path in csv_files:
            if 'N1' in file_path or 'n1' in file_path:
                self.N1问卷_list.append(file_path)
            elif 'N2' in file_path or 'n2' in file_path:
                self.N2问卷_list.append(file_path)
            elif 'N3' in file_path or 'n3' in file_path:
                self.N3问卷_list.append(file_path)
            elif 'N4' in file_path or 'n4' in file_path:
                self.N4问卷_list.append(file_path)
            elif 'N5' in file_path or 'n5' in file_path:
                self.N5问卷_list.append(file_path)
            elif 'N6' in file_path or 'n6' in file_path:
                self.N6问卷_list.append(file_path)
            elif 'N7' in file_path or 'n7' in file_path:
                self.N7问卷_list.append(file_path)
            elif 'NX' in file_path or 'nx' in file_path:
                self.N卷合并_list.append(file_path)

    # @staticmethod
    # def get_file_encoding(file_path:str) -> str:
    #     """
    #     获取文件编码格式
    #     :param file_path: 文件路径
    #     :return: 编码格式
    #     """
    #     with open(file_path, 'rb') as f:
    #         result = chardet.detect(f.read())
    #     return result['encoding']
